home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / diverses / tctnt / redtogl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-27  |  5.2 KB  |  139 lines

  1. /* REDTOGL.C: Toggle redirection
  2.  
  3.    A redirection example that toggles redirection between standard
  4.    out (STDOUT) and another output channel (NUL, CON, PRN, AUX or
  5.    a file). */
  6.  
  7. #include <process.h>        // for system()
  8. #include <bios.h>           // for biosprint()
  9. #include <errno.h>          // for errno
  10. #include <stdio.h>          // for printf()
  11. #include <fcntl.h>          // for O_RDWR, O_TRUNC and O_CREAT
  12. #include <string.h>         // for strlen()
  13. #include <sys\stat.h>       // for S_IWRITE
  14. #include <io.h>             // for open(), close(), dup() and dup2()
  15.  
  16. #define PSTATUS 2           // biosprint(): return printer status cmd
  17. #define PNUM    0           // biosprint(): 0 = LPT1:, 1 = LPT2:, etc.
  18.  
  19. int redtogl(char *redstr);  // Toggle redirect rtn prototype
  20.  
  21. //*******************************************************************
  22. void main(void) {
  23.   int status, abyte = 0;
  24.  
  25.   status = system("DIR *.C");
  26.  
  27.   printf("Output going to twilight zone (i.e., 'NUL')...\n");
  28.   if (redtogl(NULL) == -1)
  29.     printf("REDIRECT ERROR: %s\n", strerror(errno));
  30.   status = system("DIR *.C");
  31.   if (redtogl(NULL) == -1)
  32.     printf("REDIRECT ERROR: %s\n", strerror(errno));
  33.  
  34.   printf("Output going to file 'JUNK'....\n");
  35.   if (redtogl("JUNK") == -1) // Redirect output to file called "JUNK"
  36.     printf("REDIRECT ERROR: %s\n", strerror(errno));
  37.   status = system("DIR *.C");
  38.   if (redtogl(NULL) == -1)   // Parameter is ignored; restore STDOUT
  39.     printf("REDIRECT ERROR: %s\n", strerror(errno));
  40.  
  41.   /* 'PRN' (printer) and 'CON' (console) are standard DOS devices.
  42.      Since output to 'CON' has same effect as output to 'STDOUT'
  43.      we won't bother demonstrating it.
  44.  
  45.      Because trying to output to a non-existent or non-selectable
  46.      printer would cause DOS to display the infamous "Abort, Retry,
  47.      Fail" error message, we'll use the 'biosprint()' function to
  48.      skip redirected output if the printer can't be used (not
  49.      selected, out of paper, etc.) */
  50.  
  51.   printf("Will attempt to send output to PRN.\n");
  52.   status = biosprint(PSTATUS, abyte, PNUM);
  53.   if ((status & 0xBF) == 0x90) {
  54.     if (redtogl("PRN") == -1) // Try redirecting to DOS List device
  55.       printf("REDIRECT ERROR: %s\n", strerror(errno));
  56.         status = system("DIR *.C");
  57.       if (redtogl("PRN") == -1) // ignore parameter & restore STDOUT
  58.         printf("REDIRECT ERROR: %s\n", strerror(errno));
  59.   }
  60.   else
  61.     printf("PRN not ready. Skipping redirected output to PRN.\n");
  62.  
  63.   printf("End of program.\n"); }  // end of main()
  64.  
  65. /*-------------------------------------------------------------------
  66.   redtogl - Alternates (toggles) standard output between the 'NUL'
  67.             or user specified output device and the currently set
  68.             standard output device (in handle 1). If a null
  69.             (zero-length) string is passed to this routine on even
  70.             numbered calls, output is sent to 'NUL' which effectively
  71.             suppresses output.  If a valid DOS device name (CON,
  72.             PRN, AUX, NUL) or a file name is passed to the routine
  73.             output is redirected to the named device.
  74.  
  75.             Odd numbered calls restores (toggles) output back to
  76.             the device that was set on the last call to the routine
  77.             (probably the DOS console device, i.e., 'CON').
  78.  
  79.   SYNOPSIS: redtogl(tostring)
  80.     char *tostring - destination of redirected output or NULL
  81.  
  82.   RETURNS: Current standard output handle if successful or -1
  83.            on failure and one of the following in global 'errno'
  84.            variable:
  85.  
  86.              ENOENT  - no such file or directory
  87.              EMFILE  - too many open files
  88.              EACCES  - permission denied
  89.              EINVACC - invalid access code
  90.              EBADF   - bad file number */
  91.  
  92. int redtogl(char *redstr) {
  93.   /* NOTE: 'oldstdout' is static because we have to retain it
  94.            until the next call to 'redtogl' when it is used to
  95.            restore the original output device, that is, so the
  96.            routine can toggle redirection. */
  97.  
  98.   static int oldstdout;  // save handle of std output device here
  99.   int nul;               // put handle for redirect device here
  100.  
  101. #define STDOUT  1
  102.  
  103. /* 'oldstdout' is static so we can depend on the compiler to
  104.    initialize it to 0. Subsequent calls to redtogl() assure it
  105.    is maintained so we can safely disable the 'Possible use of
  106.    oldstdout before definition' warning. Note, however, that you
  107.    should always clearly understand why a warning is being
  108.    generated before disabling it! */
  109. #pragma warn -def
  110.  
  111.   if (!oldstdout) {
  112.     if (!strlen(redstr))
  113.       nul = open("NUL", O_RDWR);
  114.     else
  115.       nul = open(redstr, O_CREAT|O_TRUNC|O_RDWR, S_IWRITE);
  116.  
  117.     if (nul < 0) {
  118.       close(nul);
  119.       return(-1);
  120.     }
  121.     else {
  122.       oldstdout = dup(STDOUT);
  123.       dup2(nul, STDOUT);
  124.       close(nul);
  125.     }
  126.   }
  127.   else {
  128.     dup2(oldstdout, STDOUT);
  129.     oldstdout = close(oldstdout);
  130.   }
  131.   return(nul);
  132.  
  133. /* This restores the 'possible use of xxxx before definition'
  134.    warning to its state before it was disabled it; i.e., if it
  135.    was on it is enabled else it is left disabled. This assures
  136.    that later occurances of such warnings aren't missed. */
  137. #pragma warn .def
  138. }  // end of redtogl()
  139.